package com.xzmzhp.controller.maintain;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.maintain.Breakage;
import com.xzmzhp.service.maintain.BreakageService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-24 16:55
 * @program: ams
 * @description: 破损记录前端交互类
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/breakage")
public class BreakageController {

    @Autowired
    private BreakageService breakageService;

    /**
     * 根据参数查询数据sql
     * @param param 参数
     *        itemId 物品id
     *        itemName 物品名称
     *        stdmodeId  物品类型id
     *        breakageType 破损状态
     *        startTime 开始时间
     *        endTime  结束时间
     * @return 查询数据sql
     */
    @RequestMapping("/findOrnamentsTypeList")
    public ReturnData findBreakageList(@RequestParam(value = "page", defaultValue = "1") int page,
                                            @RequestParam(value = "limit", defaultValue = "10") int limit,
                                            @RequestBody Map<String,Object> param){
        PageHelper.startPage(page, limit, true);
        List<Breakage> list = breakageService.findBreakageList(param);
        if (CollUtil.isNotEmpty(list)){
            PageInfo<Breakage> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询破损信息失败,请重新查询");
    }

    /**
     * 添加破损信息
     * @param breakage 破损信息
     * @return 是否成功
     */
    @RequestMapping("/addStdmode")
    public ReturnData addStdmode(@RequestBody Breakage breakage){
        Integer i = breakageService.addBreakage(breakage);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        }
        return ReturnData.error(ReturnConstant.FAIL, "破损添加错误");
    }

    /**
     * 修改破损信息
     * @param breakage 破损信息
     * @return 是否成功
     */
    @RequestMapping("/updateStdmode")
    public ReturnData updateStdmode(@RequestBody Breakage breakage){
        Integer i = breakageService.updateBreakage(breakage);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "破损修改错误");
        }
    }

    /**
     * 根据破损类型id修改破损状态
     * @param breakageId 破损id
     * @param status 破损状态
     * @return 是否成功
     */
    @RequestMapping("/exitStatus")
    public ReturnData exitStatus(@RequestParam(value = "breakageId")Integer breakageId,
                                 @RequestParam(value = "status") String status){
        Integer i = breakageService.exitStatus(breakageId,status);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        }
        return ReturnData.error(ReturnConstant.FAIL, "破损状态修改错误");
    }
}
